<template>
  <div ref="echartsPie" class="echartsPie"></div>
</template>
<script setup>
import { ref, onMounted, watch } from "vue";
//  按需引入 echarts
import * as echarts from "echarts";
import "echarts-gl";
const echartsPie = ref(); // 使用ref创建虚拟DOM引用，使用时用main.valued

let props = defineProps({
  options: Object,
  titleName: String,
});
let myChart = null;
onMounted(() => {
  window.addEventListener("resize", resizeChart);
});

function resizeChart() {
  if (myChart) {
    myChart.resize();
  }
}

watch(
  () => props.options,
  (newval, oldval) => {
    init(newval);
  },
  {
    deep: true,
  }
);

async function init(newval) {
  // 基于准备好的dom，初始化echarts实例
  if (!myChart) {
    myChart = echarts.init(echartsPie.value);
  }
  const seriesData1 = newval.yh;
  const seriesData2 = newval.zg;
  const seriesData3 = newval.pc;
  const xAxisList = newval.xAxis;
  // 传入数据生成 option
  const option = {
    backgroundColor: "#fff",
    color: [
      {
        type: "linear",
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          { offset: 0, color: "#2D9BFC " }, // 0% 处的颜色
          { offset: 1, color: "#2D9BFC " }, // 100% 处的颜色
        ],
      },
      {
        type: "linear",
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          { offset: 0, color: "#28BC59" }, // 0% 处的颜色
          { offset: 1, color: "#28BC59" }, // 100% 处的颜色
        ],
      },
      {
        type: "linear",
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          { offset: 0, color: "#FFCF37" }, // 0% 处的颜色
          { offset: 1, color: "#FFCF37" }, // 100% 处的颜色
        ],
      },
    ],
    title: {
      text: "",
    },
    tooltip: {
      trigger: "item",
    },
    grid: {
      left: "3%",
      right: "0%",
      bottom: "5%",
      containLabel: true,
    },
    legend: {
      orient: "horizontal",
      left: "right",
      data: ["隐患数", "已整改", "排查次数"],
      textStyle: {
        color: "#999",
      },
      itemWidth: 20,
      itemHeight: 3,
    },
    xAxis: {
      type: "category",
      data: xAxisList,
      axisLabel: {
        color: "#999",
      },
      axisTick: {
        show: false,
      },
      axisLine: {
        show: false,
      },
    },
    yAxis: {
      type: "value",
      axisLabel: {
        color: "#999",
      },
      splitLine: {
        show: true,
        lineStyle: {
          color: ["#979797"],
          opacity: 0.21,
          width: 1,
          type: "dashed",
        },
      },
    },
    series: [
      {
        barWidth: 8,
        name: "隐患数",
        type: "bar",
        data: seriesData1,
      },
      {
        barWidth: 8,
        name: "已整改",
        type: "bar",
        data: seriesData2,
      },
      {
        barWidth: 8,
        name: "排查次数",
        type: "bar",
        data: seriesData3,
      },
    ],
  };

  myChart.setOption(option);
}
</script>
<style scoped>
.echartsPie {
  width: 100%;
  height: 100%;
}
</style>
